package com.sg.service.biz.app.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.app.req.ImplementInitSpaceConfComReqDto;
import com.sg.dto.biz.app.req.TransitionConfItemMatchAnswerReqDto;
import com.sg.dto.biz.app.req.TransitionConfItemResultToAnswerReqDto;
import com.sg.dto.biz.app.res.ImplementInitSpaceConfComRespDto;
import com.sg.dto.biz.app.res.TransitionConfItemMatchAnswerRespDto;
import com.sg.dto.biz.app.res.TransitionConfItemResultToAnswerRespDto;
import com.sg.dto.integration.component.QueryInitSpaceConfDetailComReqDto;
import com.sg.dto.integration.component.QueryInitSpaceConfDetailComRespDto;
import com.sg.dto.integration.framework.FrameworkSpaceDto;
import com.sg.dto.integration.framework.QuerySpaceListComReqDto;
import com.sg.dto.integration.framework.QuerySpaceListComRespDto;
import com.sg.service.base.app.MOmsTaskService;
import com.sg.service.base.app.MSpaceService;
import com.sg.service.biz.app.InterfaceModeService;
import com.sg.common.exception.Assert;
import com.wicket.okrcalc.integration.InterfaceModeService;
import com.wicket.okrcomponent.integration.ConfSchemeService;
import com.wicket.okrcomponent.integration.InterfaceModeService;
import com.wicket.okrfinance.integration.InterfaceModeService;
import com.wicket.okrframework.integration.BaseService;
import com.wicket.okrframework.integration.InterfaceModeService;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class ConfSchemeServiceImpl
        implements com.wicket.okrapp.biz.service.ConfSchemeService {

    @Resource
    private RedisUtil redisUtil;
    //@Resource
    //private MCustomFieldsService mCustomFieldsService;
    @Resource
    private NbConfScheme nbConfScheme;
    @Resource
    private MSpaceService mSpaceService;
    //@Resource
    //private undefinedService undefinedService;
    @Resource
    private BaseService baseService;
    @Resource
    private ConfSchemeService confSchemeService;
    @Resource
    private InterfaceModeService interfaceModeService;
    @Resource
    private MOmsTaskService mOmsTaskService;
    @Resource
    private InterfaceModeService fwBaseInterfaceModeClient;
    @Resource
    private InterfaceModeService interfaceModeService;
    @Resource
    private InterfaceModeService fwCalcInterfaceModeClient;
    @Resource
    private InterfaceModeService fwFinanceInterfaceModeClient;

    /**
     * D3转换配置项匹配答案（特殊方法）[2576]
     * gen by moon at 7/5/2023, 1:49:30 AM
     */
    @Trace(operationName = "D3转换配置项匹配答案（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public TransitionConfItemMatchAnswerRespDto transitionConfItemMatchAnswer(TransitionConfItemMatchAnswerReqDto reqDto) {


        TransitionConfItemResultToAnswerRespDto rowToColumnRes_1 = null;
//步骤0: M3转换配置项结果转答案（特殊方法） - transitionConfItemResultToAnswer
        //ModelCode: rowToColumn
        TransitionConfItemResultToAnswerRespDto rowToColumnRes = null;
        TransitionConfItemResultToAnswerReqDto rowToColumnReq = new TransitionConfItemResultToAnswerReqDto();
        if (reqDto != null && reqDto.getConfSchemeItemResultList() != null && !CollectionUtil.isEmpty(reqDto.getConfSchemeItemResultList())) {
            rowToColumnReq.setConfSchemeItemResultList(reqDto.getConfSchemeItemResultList().stream().map(item -> BeanUtil.toBean(item, ConfSchemeItemResultDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:79730_1
        }

        /*M3转换配置项结果转答案（特殊方法）[2448]  导入变更字段使用，将excel每行数据转换成列 */

        rowToColumnRes = nbConfScheme.transitionConfItemResultToAnswer(rowToColumnReq);


        rowToColumnRes_1 = rowToColumnRes;

        TransitionConfItemMatchAnswerRespDto retData = new TransitionConfItemMatchAnswerRespDto();
        if (rowToColumnRes_1 != null) {
            retData.setConfSchemeId(rowToColumnRes_1.getConfSchemeId());//SimpleFieldAssign//sourceId:79781_1
            retData.setFatherCycleType(rowToColumnRes_1.getFatherCycleType());//SimpleFieldAssign//sourceId:79782_1
            retData.setFatherSubcycleQuantity(rowToColumnRes_1.getFatherSubcycleQuantity());//SimpleFieldAssign//sourceId:79784_1
            retData.setMidCycleType(rowToColumnRes_1.getMidCycleType());//SimpleFieldAssign//sourceId:79785_1
            retData.setMidSubcycleQuantity(rowToColumnRes_1.getMidSubcycleQuantity());//SimpleFieldAssign//sourceId:79786_1
            retData.setSubcycleType(rowToColumnRes_1.getSubcycleType());//SimpleFieldAssign//sourceId:79787_1
            retData.setCycleMode(rowToColumnRes_1.getCycleMode());//SimpleFieldAssign//sourceId:79789_1
            retData.setHeadCopywriter(rowToColumnRes_1.getHeadCopywriter());//SimpleFieldAssign//sourceId:79793_1
            retData.setEvaThemeSketch(rowToColumnRes_1.getEvaThemeSketch());//SimpleFieldAssign//sourceId:79794_1
            retData.setButtonCopywriter(rowToColumnRes_1.getButtonCopywriter());//SimpleFieldAssign//sourceId:79795_1
            retData.setResultCycleType(rowToColumnRes_1.getResultCycleType());//SimpleFieldAssign//sourceId:79796_1
            retData.setUseObjectTypeCode(rowToColumnRes_1.getUseObjectTypeCode());//SimpleFieldAssign//sourceId:79818_1
            retData.setIsOpenPlanSchemeClass(rowToColumnRes_1.getIsOpenPlanSchemeClass());//SimpleFieldAssign//sourceId:79932_1
            retData.setCycleSystemCode(rowToColumnRes_1.getCycleSystemCode());//SimpleFieldAssign//sourceId:79820_1
            retData.setIsOpenBudget(rowToColumnRes_1.getIsOpenBudget());//SimpleFieldAssign//sourceId:90642_1
            retData.setAccountUnit(rowToColumnRes_1.getAccountUnit());//SimpleFieldAssign//sourceId:90643_1
            retData.setBudgetHierarchy(rowToColumnRes_1.getBudgetHierarchy());//SimpleFieldAssign//sourceId:90644_1
        }


        return retData;
    }

    /**
     * D3执行初始化空间配置(公共)[9604]
     * gen by moon at 5/19/2024, 2:07:24 AM
     */
    @Trace(operationName = "D3执行初始化空间配置(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ImplementInitSpaceConfComRespDto implementInitSpaceConfCom(ImplementInitSpaceConfComReqDto reqDto) {


        //virtualUsage D1-2查询空间列表(管理)(公共)  68571
        QuerySpaceListComRespDto querySpaceListComRespDto = null;
        QuerySpaceListComReqDto querySpaceListComReqDto = new QuerySpaceListComReqDto();


        /*D1-2查询空间列表(管理)(公共)[3315]   */

        querySpaceListComRespDto = baseService.querySpaceListCom(querySpaceListComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        if ((querySpaceListComRespDto != null && querySpaceListComRespDto.getSpaceList() != null && querySpaceListComRespDto.getSpaceList().size() > 0)) {
            //if(D1-2查询空间列表(管理)(公共).空间列表数据集条数 大于 0)  68572

//ModelCode: circulationCollections
            for (FrameworkSpaceDto circulationCollectionsRes : querySpaceListComRespDto.getSpaceList()) {

                QueryInitSpaceConfDetailComRespDto queryInitSpaceConfDetailComRespDto = null;
                QueryInitSpaceConfDetailComReqDto queryInitSpaceConfDetailComReqDto = new QueryInitSpaceConfDetailComReqDto();
                if (circulationCollectionsRes != null) {
                    queryInitSpaceConfDetailComReqDto.setSpaceId(circulationCollectionsRes.getSpaceId());//SimpleFieldAssign//sourceId:1807881_1_68577
                }

                /*D2查询初始化空间配置(公共)[9610]   */
                Assert.isNull(queryInitSpaceConfDetailComReqDto.getSpaceId(), "D3执行初始化空间配置(公共)-D2查询初始化空间配置(公共)-空间ID不能为空", false);
                queryInitSpaceConfDetailComRespDto = confSchemeService.queryInitSpaceConfDetailCom(queryInitSpaceConfDetailComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


//ModelCode: circulationEnd
            }

        }
        ImplementInitSpaceConfComRespDto retData = new ImplementInitSpaceConfComRespDto();


        return retData;
    }


    //
}
